package com.sgb.jicai.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.JcActivityDeviceTypeBinding;
import com.sgb.jicai.view.adapter.JCMachineTypeLAdapter;
import com.sgb.jicai.view.adapter.JCMachineTypeRAdapter;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.link.entity.ReturnTypeEntity;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import java.io.Serializable;
import java.util.List;

/**
 * Time:2021/9/22
 * AUTHOR:F_Q
 * Description:
 */
public class JiCaiDeviceTypeActivity extends AppActivity {
    private JCMachineTypeLAdapter machinePublishTypeLAdapter;
    private JCMachineTypeRAdapter machinePublishTypeRAdapter;
    private List<PublicTypeEntity<List<PublicTypeEntity>>> publicTypeEntityList;
    private List<PublicTypeEntity<List<PublicTypeEntity>>> publicTypeEntityChildList;
    private String firstName;
    private String secondName;
    private String firstNo;
    private String secondNo;
    private ReturnTypeEntity returnTypeEntity;
    private JcActivityDeviceTypeBinding binding;

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.jc_activity_device_type);
        binding.includeTitle.ivBack.setOnClickListener(v -> finish());
        binding.includeTitle.titleView.setText("类型选择");
    }

    @Override
    protected void init() {
        returnTypeEntity = new ReturnTypeEntity();
        publicTypeEntityList = (List<PublicTypeEntity<List<PublicTypeEntity>>>) getIntent().getSerializableExtra("publicTypeEntityList");
        machinePublishTypeLAdapter = new JCMachineTypeLAdapter(JiCaiDeviceTypeActivity.this, null);
        machinePublishTypeLAdapter.setItemSelect(true);
        machinePublishTypeLAdapter.setDefItem(0);
        binding.leftRecycler.setLayoutManager(new LinearLayoutManager(JiCaiDeviceTypeActivity.this));
        binding.leftRecycler.setAdapter(machinePublishTypeLAdapter);
        machinePublishTypeLAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity<List<PublicTypeEntity>>>() {
            @Override
            public void onItemClick(PublicTypeEntity<List<PublicTypeEntity>> publicTypeEntity, int position) {
                firstName = publicTypeEntity.getName();
                firstNo = publicTypeEntity.getNo();
                machinePublishTypeRAdapter.setDatas(publicTypeEntity.getChildren());
            }
        });

        machinePublishTypeRAdapter = new JCMachineTypeRAdapter(JiCaiDeviceTypeActivity.this, null);
        machinePublishTypeRAdapter.setItemSelect(true);
        machinePublishTypeRAdapter.setDefItem(0);
        binding.rightRecycler.setLayoutManager(new GridLayoutManager(JiCaiDeviceTypeActivity.this, 3));
        binding.rightRecycler.setAdapter(machinePublishTypeRAdapter);
        machinePublishTypeRAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity>() {
            @Override
            public void onItemClick(PublicTypeEntity publicTypeEntity, int position) {
                secondName = publicTypeEntity.getName();
                secondNo = publicTypeEntity.getNo();
                if (TextUtils.isEmpty(firstName)) {
                    returnTypeEntity.setFirstName(publicTypeEntityList.get(0).getName());
                    returnTypeEntity.setFirstNo(publicTypeEntityList.get(0).getNo());
                } else {
                    returnTypeEntity.setFirstName(firstName);
                    returnTypeEntity.setFirstNo(firstNo);
                }
                returnTypeEntity.setSecondName(secondName);
                returnTypeEntity.setSecondNo(secondNo);
                Intent intent = new Intent();
                intent.putExtra("data", (Serializable) returnTypeEntity);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        setDate();
    }

    public void getMachineTypeSucc(List<PublicTypeEntity<List<PublicTypeEntity>>> datas) {
        this.publicTypeEntityChildList = datas;
    }

    public void setDate() {
        for (int i = 0; i < publicTypeEntityList.size(); i++) {
            Log.e("tag数据",  publicTypeEntityList.get(i).getName());
        }
        machinePublishTypeLAdapter.setDatas(publicTypeEntityList);
        if (publicTypeEntityList != null) {
            machinePublishTypeRAdapter.setDatas(publicTypeEntityList.get(0).getChildren());
            String no = publicTypeEntityList.get(0).getNo();
        }
    }
}
